<?php

class Core_Action_Admin_System_I18n_Datetime_Edit extends Action_Admin
{
	public function execute($request, $initial = false, $script = null)
	{
		if ($initial)
		{
			Agilet::setVar('breadcrum', array(
				array(
					'url' => 'admin/system',
					'label' => 'System'
				),
				array(
					'url' => 'admin/system/i18n',
					'label' => 'International'
				),
				array(
					'url' => 'admin/system/i18n/datetime',
					'label' => 'Date and Time'
				),
				array(
					'url' => 'admin/system/i18n/datetime/edit',
					'label' => 'Edit'
				)
			));
		}

		$table = Model::getTable('core/i18n/datetime');

		$form = Form::factory('core/admin/i18n/datetime');

		$id = $request -> getParam('id');

		if ($id && $request -> isGet())
		{
			$item = $table -> find($id) -> current();
			$form -> setData($item -> toArray());
		}

		if ($request -> isPost() and $form -> isValid($_POST))
		{
			$data = $form -> getData();

			if ($id)
			{
				$item = $table -> find($id) -> current();
			}
			
			/**
			 * create new item if not exists
			 */
			if (!$item)
			{
				$item = $table -> fetchNew();
			}

			$item -> fromArray($data);
			$item -> save();
			
			/**
			 * clear enbale/disable language cache.
			 */
			Cache::flush();
		
			/**
			 * redirect to mangae language page.
			 */
			Response::initiator() -> redirect('admin/system/i18n/datetime');
		}

		$request -> response = View::render($script = 'core/html/admin/edit', $data = array('form' => $form));
	}

}
